/*
 * @Descripttion:
 * @version:
 * @@Company: DCIT-SH
 * @Author: Sunny Chen
 * @Date: 2024-07-29 22:49:59
 * @LastEditors: Sunny Chen
 * @LastEditTime: 2024-07-31 16:11:51
 */
import * as Cesium from "cesium";

/**
 *
 * @param {Array<Object>} positions 存储敬畏对对象的数组
 * @returns 经纬度对象扁平化后的数组
 */
export const flattenPositions = (positions) => {
  const arraryOfLnglat = [];
  positions.forEach((position) => {
    arraryOfLnglat.push(position.lng);
    arraryOfLnglat.push(position.lat);
  });
  return arraryOfLnglat;
};
//计算每个点位时间与总时间
// pArr:[{x,y,z}]
// speed number
export const getSiteTimes = (pArr, speed) => {
  let timeSum = 0,
    times = [];
  for (var i = 0; i < pArr.length; i++) {
    if (i == 0) {
      times.push(0); //第0个时间为0
      continue;
    }

    // 计算总时间
    timeSum += spaceDistance([pArr[i - 1], pArr[i]]) / speed;
    // 将每段距离所用的时间收集起来
    times.push(timeSum);
  }
  return {
    timeSum: timeSum,
    siteTimes: times,
  };
};

//计算距离
export const spaceDistance = (positions) => {
  let distance = 0;
  for (let i = 0; i < positions.length - 1; i++) {
    let s = Cesium.Cartesian3.distance(positions[i], positions[i + 1]);
    distance = distance + s;
  }
  return distance.toFixed(2);
};

/**
 * 获得圆边缘坐标
 * @param {Array} center 圆心坐标
 * @param {Number} radius 半径
 * @return {Array} 边缘坐标数组
 */
export const getCirclePoints = (center, radius) => {
  let points = [];
  for (let i = 0; i <= 360; i += 2) {
    points.push(getCirclePoint(center[0], center[1], i, radius));
  }
  return points;
};

const getCirclePoint = (lon, lat, angle, radius) => {
  let dx = radius * Math.sin((angle * Math.PI) / 180.0);
  let dy = radius * Math.cos((angle * Math.PI) / 180.0);
  let ec = 6356725 + ((6378137 - 6356725) * (90.0 - lat)) / 90.0;
  let ed = ec * Math.cos((lat * Math.PI) / 180);
  let newLon = ((dx / ed + (lon * Math.PI) / 180.0) * 180.0) / Math.PI;
  let newLat = ((dy / ec + (lat * Math.PI) / 180.0) * 180.0) / Math.PI;
  return [newLon, newLat];
};
